There are two kinds of statements in C programs, statements which do something (i.e. ones which make something happen or change the contents of a variable) and statements which change the direction the program is running in. You need to do this because your program will need to alter what it does in response to the values that it sees. In the projects we find that we want the program to do different things when buttons are pressed, and we use conditional statements to do this.

The simplest form of conditional statement you can have is:

if ( condition ) statement ;

The statement is obeyed if the condition evaluates as true when the program runs.

This form of the construction only allows a single statement to be performed if the condition is true. We can expand this to:

if ( condition )
{
    statement ;
    statement ;
}

I do this by using the braces (curly brackets) to create a block after the if which contains the statements to be obeyed. Note that I have used a particular layout to make it clear that the execution of the statements is dependent on the state of the condition.

C and Logical Values

C knows about true and false, and will do things depending on whether something is true or false. However, C is very simple minded in that it regards the value 0 as false and any other value as true.

This means that as far as C is concerned the value 99 is true, as is the value 98 and any other non-zero expression. This means that I could write something like:

counter - 99

This is an expression. It says "work out the value of the variable counter minus 99". (remember that - is an operator which is being applied between the constant 99 and the variable counter). The result will depend on what is in the variable counter. If (and only if) counter holds the value 99 the result of this expression will be zero, which we know C regards as false.

This means that I can make decisions using the - operator. Note that this expression does not actually change the value in the variable counter, since we have not performed an assignment to that variable, merely done a calculation using the value in it.

You must remember that negative numbers are also non-zero, and so would be regarded as true by C.

On the right we have a program which will execute three conditional constructions. See if you can decide which would be performed and then run the program to find out. The first two should be easy, the third should also be easy if you remember that negative values are not zero!